package com.hmall.search.service.impl;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmall.common.dto.Address;
import com.hmall.common.dto.Item;
import com.hmall.common.feign.ItemClient;
import com.hmall.common.feign.UserClient;
import com.hmall.search.mapper.OrderDetailMapper;
import com.hmall.search.mapper.OrderLogisticsMapper;
import com.hmall.search.mapper.OrderMapper;
import com.hmall.search.pojo.Order;
import com.hmall.search.pojo.OrderDetail;
import com.hmall.search.pojo.OrderLogistics;
import com.hmall.search.pojo.OrderPayload;
import com.hmall.search.service.IOrderService;
import com.hmall.search.utils.ThreadLocalUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;


@Service
public class OrderService extends ServiceImpl<OrderMapper, Order> implements IOrderService {

    @Autowired
    private ItemClient itemClient;
    @Autowired
    private UserClient userClient;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private OrderDetailMapper orderDetailMapper;
    @Autowired
    private OrderLogisticsMapper orderLogisticsMapper;

    /**
     * 创建订单
     */
    @Override
    @Transactional
    public Order submit(@RequestBody OrderPayload orderPayload) {
        //获得下单商品ID
        Long itemId = orderPayload.getItemId();
        //下单商品数量
        Integer num = orderPayload.getNum();
        //商品下单支付方式
        Integer paymentType = orderPayload.getPaymentType();
        //获得下单商品信息
        Long addressId = orderPayload.getAddressId();
        Item item = itemClient.getById(itemId);
        Long price = item.getPrice();
        //计算下单商品总价格
        Long totalFee;
        totalFee = (price * num);
        //获取用户id
        String useId = ThreadLocalUtils.getUseId();
        //封装Order保存订单
        Order order = new Order();
        //雪花算法计算出来的ID
        long id = IdWorker.getId();

        //order.setId(itemId);
        order.setTotalFee(totalFee);//商品总价
        order.setPaymentType(paymentType);//支付方式
        order.setUserId(Long.valueOf(useId));//下单人
        order.setStatus(1);//订单状态,未支付

        //保存到数据库
        orderMapper.insert(order);


        /**
         * 保存订单明细
         */
        OrderDetail orderDetail = new OrderDetail();
        orderDetail.setOrderId(order.getId());
        orderDetail.setSpec(item.getSpec()); //商品规格数据
        orderDetail.setItemId(itemId);
        orderDetail.setNum(num);
        orderDetail.setTitle(item.getName());
        orderDetail.setPrice(item.getPrice());
        orderDetail.setImage(item.getImage());

        orderDetailMapper.insert(orderDetail);


        /**
         * 保存物流信息
         */
        Address address = userClient.findAddressById(addressId);
        OrderLogistics orderLogistics = new OrderLogistics();
        orderLogistics.setOrderId(order.getId());
        //   orderLogistics.setLogisticsNumber(UUID.randomUUID().toString());
        //   orderLogistics.setContact(address.getContact());
           orderLogistics.setPhone(address.getMobile());
        //   orderLogistics.setProvince(address.getProvince());
        //   orderLogistics.setCity(address.getCity());
        //   orderLogistics.setTown(address.getTown());
        //   orderLogistics.setStreet(address.getStreet());
        BeanUtils.copyProperties(address,orderLogistics);

        orderLogisticsMapper.insert(orderLogistics);
        /**
         * 扣减库存
         */

        itemClient.stock(itemId, num);

        return order;

    }


}
